var roleService = require('../../service/roleService');
/**
 * 角色控制器 
 * Author: 梦里小白龙 <jiangwenshu1990@126.com> <http://www.jz07.cn>
 */

module.exports  = {
    //添加角色
    save ( req, res, next){
        console.log("params",req.body);
        let params = {
            role_name:req.body.role_name,
            stats:req.body.stats,
            role_txt:req.body.role_txt,
            role_module:req.body.role_module.toString()
        }
        
        //添加角色信息
        roleService.save(params).then(data=>{
            data ? res.json({code:1 ,msg:'添加成功'}) : res.json({code:0 ,msg:'添加失败'});
        });
    },
    //更新数据
    update(req ,res, next){
        let params = {
            role_name:req.body.role_name,
            stats:req.body.stats,
            role_txt:req.body.role_txt,
            role_module:req.body.role_module.toString(),
            id:req.body.id,
        }
        //添加角色信息
        roleService.update(params).then(data=>{
            data ? res.json({code:1 ,msg:'修改成功'}) : res.json({code:0 ,msg:'修改失败'});
        });
    },
    //查询角色信息
    getById(req, res, next){
        if(!req.body.id){
            res.json({code:2004 ,msg:'参数错误'})
        } 
        //查询角色信息
        roleService.getById(req.body.id).then(data=>{
            data ? res.json({code:1 ,msg:'查询成功',info: data}) : res.json({code:0 ,msg:'查询失败'});
        })
    },
    //删除角色数据
    delete(req, res, next){
        
        let roleId = req.body.id;

        //查询用户是否包含该角色
        roleService.selectUserRole(roleId).then(response=>{
            if(response.length >= 1){
                res.json({code:-1 ,msg:'该角色被用户使用'})
            }else{
                //删除角色信息
                roleService.udelete(roleId).then(data=>{
                    data ? res.json({code:1 ,msg:'删除成功'}) : res.json({code:0 ,msg:'删除失败'});
                })
            }
        })
  
    },
}